package org.jeecg.modules.base.controller;

import com.baomidou.mybatisplus.extension.service.IService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class JeecgController<T, S extends IService<T>> extends org.jeecg.common.system.base.controller.JeecgController {

    @Autowired
    private SysUserMapper  sysUserMapper;

    public String getExportFields(Class cls,String permsPrefix) {
        //获取当前登录人
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //权限配置列导出示例
        List<String> noAuthList = new ArrayList<>();
        List<String> exportFieldsList = new ArrayList<>();
        //1.此前缀必须与列表字段权限控制前缀一致
        //查询配置菜单有效字段
        List<String> allAuth = this.sysUserMapper.queryAllAuth(permsPrefix);
        //查询已授权字段
        List<String> userAuth = this.sysUserMapper.queryUserAuth(sysUser.getId(),permsPrefix);
        //列出未授权字段
        for(String perms : allAuth){
            if(!userAuth.contains(perms)){
                noAuthList.add(perms.substring(permsPrefix.length()));
            }
        }
        //实体类中字段与未授权字段比较，列出需导出字段
        Field[] fileds = cls.getDeclaredFields();
        List<Field> list = new ArrayList(Arrays.asList(fileds));
        for(Field field : list){
            if(!noAuthList.contains(field.getName())){
                exportFieldsList.add(field.getName());
            }
        }
        return exportFieldsList != null && exportFieldsList.size()>0 ? String.join(",", exportFieldsList) : "";
    }
}
